<template>
    <div>
        <p class="tip">DLHTX导航,欢迎您的加入</p>
        <!-- 登录页面 -->
        <div class="cont" v-if='$pcShow'>
            <div class="form sign-in">
                <h2>登录畅想云端同步</h2>
                <label>
                    <span>用户名/邮箱</span>
                    <input type="text" v-model="signInName" AUTOCOMPLETE="on">
                </label>
                <label>
                    <span>密码</span>
                    <input type="password" v-model="signInPassword" AUTOCOMPLETE="on">
                </label>
                <p class="forgot-pass">忘记密码?</p>
                <button type="button" class="submit" @click="fnLogin()">登录</button>
                <button type="button" class="fb-btn" @click='loginGit()'>
                   访问DLHTX博客
                </button>
            </div>
            <div class="sub-cont">
                <div class="img">
                    <div class="img__text m--up">
                        <h2>新用户?</h2>
                        <p>赶快登录和探索我们的DLHTX博客吧!</p>
                    </div>
                    <div class="img__text m--in">
                        <h2>已经有账号了?</h2>
                        <p>如果你已经有我们的账号了,那么你还在等什么,赶快登录吧!</p>
                    </div>
                    <div class="img__btn">
                        <span class="m--up">注册</span>
                        <span class="m--in">登录</span>
                    </div>
                </div>
                <!-- 注册页面 -->
                <div class="form sign-up">
                    <h2>赶快注册吧</h2>
                    <label>
                        <span>用户名</span>
                        <input type="text" v-model="registerName">
                    </label>
                    <label>
                        <span>邮箱</span>
                        <input type="email" v-model="registerEmail">
                    </label>
                    <label>
                        <span>密码</span>
                        <input type="password" v-model="registerPassword">
                    </label>
                    <label>
                        <span>确认密码</span>
                        <input type="password" v-model="registerPassword2">
                    </label>
                    <label>
                        <span>验证码</span>
                        <span
                            style="background: #4f504c;color: white;padding: 3px 10px;border-radius: 16PX;cursor: pointer;"
                            @click="getCode()" v-if="time==0">获取验证码</span>
                        <span
                            style="background:#717171;color: white!important;padding: 3px 10px;border-radius: 16PX;cursor: pointer;"
                            v-if="time>=1&&time<=60">{{time}}秒后获取</span>
                        <input type="number" v-model="registercode">
                    </label>
                    <button type="button" class="submit" @click="fnregister()">注册</button>
                    <button type="button" class="fb-btn">
                        访问DLHTX博客
                        <!-- <span>GitHub</span> -->
                    </button>
                </div>
            </div>
        </div>
        <div v-else style='width: 100%;height: 100%;'>
            <div class="form sign-in" style='width: 100%;' v-if='!showRegister'>
                <h2>欢迎回来</h2>
                <label>
                    <span>用户名/邮箱</span>
                    <input type="text" v-model="signInName">
                </label>
                <label>
                    <span>密码</span>
                    <input type="password" v-model="signInPassword" @keyup.enter='fnLogin'>
                </label>
                <p class="forgot-pass">忘记密码?</p>
                <button type="button" class="submit" @click="fnLogin()">登录</button>
                <button type="button" class="fb-btn" @click='fnshowRegister()'>注册</button>
                <button type="button" class="fb-btn" @click='loginGit()' style="margin-top: 20px;background: #eaeaea;">
                   访问DLHTX博客
                
                </button>
            </div>
            <!-- 注册页面 -->
            <div class="form sign-up"
                style='width: 1000px;height: 1000px;position: fixed;top: 6%;z-index: 999;left: 148%;'
                v-if='showRegister'>
                <h2>赶快注册吧</h2>
                <label>
                    <span>用户名</span>
                    <input type="text" v-model="registerName">
                </label>
                <label>
                    <span>邮箱</span>
                    <input type="email" v-model="registerEmail">
                </label>
                <label>
                    <span>密码</span>
                    <input type="password" v-model="registerPassword">
                </label>
                <label>
                    <span>确认密码</span>
                    <input type="password" v-model="registerPassword2">
                </label>
                <label>
                    <span>验证码</span>
                    <span
                        style="background: #4f504c;color: white;padding: 3px 10px;border-radius: 16PX;cursor: pointer;"
                        @click="getCode()" v-if="time==0">获取验证码</span>
                    <span
                        style="background:#717171;color: white!important;padding: 3px 10px;border-radius: 16PX;cursor: pointer;"
                        v-if="time>=1&&time<=60">{{time}}秒后获取</span>
                    <input type="number" v-model="registercode">
                </label>
                <button type="button" class="submit" @click="fnregister()">注册</button>
                <button type="button" class="fb-btn" @click='fnshowRegister()'>登录</button>
            </div>

        </div>
    </div>
</template>


<script>
import { mapGetters, mapActions } from "vuex";
import auth from "../../api/auth";
import Header from "../../components/Header/Header";
import Footer from "../../components/Footer/Footer";
export default {
    name: "index",
    components: {
        Header,
        Footer
    },
    data() {
        return {
            signInName: JSON.parse(localStorage.getItem('loginRemeber')) ? JSON.parse(localStorage.getItem('loginRemeber')).name : "",
            signInPassword: "",
            registerName: "",
            registerPassword: "",
            registerPassword2: "",
            Auth: "",
            registerEmail: "",
            time: 0,
            timeInterval: "",
            registercode: "",
            showRegister: false,
        };
    },
    mounted() {
        console.log(this.$route)
        if (this.$pcShow) {
            document.querySelector(".img__btn").addEventListener("click", function () {
                document.querySelector(".cont").classList.toggle("s--signup");
            });
        }
    },
    methods: {
        ...mapActions(["login", "logout", "getPermissions"]),
        showBtn() { },
        loginGit() {
            window.location.href = 'http://dlhtx.zicp.vip:9090'
        },
        fnshowRegister() {
            if (this.showRegister) {
                this.showRegister = false
            } else {
                this.showRegister = true
            }
        },
        async getCode() {
            var uPattern = /^[\da-zA-Z_\u4e00-\u9f5a]{3,16}$/;
            if (!uPattern.test(this.registerName))
                return this.$message({
                    message: "用户名为3到16位中英文(可包含中文,数字,字母和下划线)",
                    type: "warning"
                });

            var reg = /^\w+((.\w+)|(-\w+))@[A-Za-z0-9]+((.|-)[A-Za-z0-9]+).[A-Za-z0-9]+$/; //正则表达式
            if (!reg.test(this.registerEmail)) {
                return this.$message("邮箱格式错误");
            }
            if (this.registerName == "") return this.$message("用户名不可为空");
            if (this.registerEmail == "") return this.$message("邮箱不可为空");
            this.time = 60;
            this.timeInterval = setInterval(() => {
                this.time--;
                if (this.time == 0) {
                    clearInterval(this.timeInterval);
                }
            }, 1000);
            auth
                .getCode(this.registerName, this.registerEmail)
                .then(res => {
                    if (res.success) {
                        this.$message.success("验证码发送成功");
                    }
                })
                .catch(err => {
                    this.$message.error(err.errorMsg);
                    clearInterval(this.timeInterval);
                    this.time = 0
                });
        },

        async fnLogin() {
            if (this.signInName == "")
                return this.$message.error({ message: "用户名不可为空" });
            try {
                let res = await this.login({
                    name: this.signInName,
                    password: this.$md5(this.signInPassword)
                });
                if (res) {
                    if (this.$route.query.redirect) {
                        let redirect = this.$route.query.redirect;
                        console.log(redirect)
                        this.$router.push(redirect);
                        // this.$router.replace({ path: this.$route.query.redirect });
                    } else {
                        this.$router.push({ path: "/" });
                    }
                } else {
                    this.$message.error({ message: res.errorMsg });
                }
            } catch (err) {
                console.log(err);
                this.$message.error({ message: "登录失败" });
            }
        },
        async fnregister() {
            console.log("fnregister");
            var uPattern = /^[\da-zA-Z_\u4e00-\u9f5a]{3,16}$/;
            if (this.registerPassword != this.registerPassword2)
                return this.$message({ message: "两次密码不一致", type: "warning" });
            if (!uPattern.test(this.registerName))
                return this.$message({
                    message: "用户名为3到16位中英文(可包含中文,数字,字母和下划线)",
                    type: "warning"
                });
            if (this.registercode == '')
                return this.$message({
                    message: "请输入验证码",
                    type: "warning"
                });
            try {
                if (this.registerName == "" || this.registerPassword == "")
                    return this.$message({
                        message: "用户名或密码不可为空",
                        type: "warning"
                    });
                let res = await auth.register({
                    name: this.registerName,
                    password: this.$md5(this.registerPassword),
                    code: this.registercode
                });
                if (res.success) {
                    this.$message({ message: "注册成功", type: "success" });
                    this.signInName = this.registerName;
                    if (this.$pcShow) {
                        document.querySelector(".cont").classList.toggle("s--signup");
                    } else {
                        this.fnshowRegister()
                    }
                } else {

                }
            } catch (err) {
                console.log(err);
                this.$message.error({ message: err.errorMsg });
            }
        },
        goRegister() {
            this.$router.push("/register");
        }
    },
    computed: {
        ...mapGetters(["isLogin", "user"])
    }
};
</script>

<style scoped lang='scss'>
*,
*:before,
*:after {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

body {
    font-family: "Open Sans", Helvetica, Arial, sans-serif;
    background: #ededed;
}

input,
button {
    border: none;
    outline: none;
    background: none;
    font-family: "Open Sans", Helvetica, Arial, sans-serif;
}

$contW: 900px;
$imgW: 260px;
$formW: $contW - $imgW;
$switchAT: 1.2s;

$inputW: 260px;
$btnH: 36px;

$diffRatio: ($contW - $imgW) / $contW;

@mixin signUpActive {
    .cont.s--signup & {
        @content;
    }
}

.tip {
    font-size: 20px;
    margin: 40px auto 50px;
    text-align: center;
}

.cont {
    overflow: hidden;
    position: relative;
    width: $contW;
    height: 550px;
    margin: 0 auto 100px;
    background: #fff;
}

.form {
    position: relative;
    width: $formW;
    height: 100%;
    transition: transform $switchAT ease-in-out;
    padding: 50px 30px 0;
}

.sub-cont {
    overflow: hidden;
    position: absolute;
    left: $formW;
    top: 0;
    width: $contW;
    height: 100%;
    padding-left: $imgW;
    background: #fff;
    transition: transform $switchAT ease-in-out;

    @include signUpActive {
        transform: translate3d($formW * -1, 0, 0);
    }
}

button {
    display: block;
    margin: 0 auto;
    width: $inputW;
    height: $btnH;
    border-radius: 30px;
    color: #fff;
    font-size: 15px;
    cursor: pointer;
}

.img {
    overflow: hidden;
    z-index: 2;
    position: absolute;
    left: 0;
    top: 0;
    width: $imgW;
    height: 100%;
    padding-top: 360px;

    &:before {
        content: "";
        position: absolute;
        right: 0;
        top: 0;
        width: $contW;
        height: 100%;
        background-image: url("https://s3-us-west-2.amazonaws.com/s.cdpn.io/142996/sections-3.jpg");
        background-size: cover;
        transition: transform $switchAT ease-in-out;
    }

    &:after {
        content: "";
        position: absolute;
        left: 0;
        top: 0;
        width: 100%;
        height: 100%;
        background: rgba(0, 0, 0, 0.6);
    }

    @include signUpActive {
        &:before {
            transform: translate3d($formW, 0, 0);
        }
    }

    &__text {
        z-index: 2;
        position: absolute;
        left: 0;
        top: 50px;
        width: 100%;
        padding: 0 20px;
        text-align: center;
        color: #fff;
        transition: transform $switchAT ease-in-out;

        h2 {
            margin-bottom: 10px;
            font-weight: normal;
        }

        p {
            font-size: 14px;
            line-height: 1.5;
        }

        &.m--up {
            @include signUpActive {
                transform: translateX($imgW * 2);
            }
        }

        &.m--in {
            transform: translateX($imgW * -2);

            @include signUpActive {
                transform: translateX(0);
            }
        }
    }

    &__btn {
        overflow: hidden;
        z-index: 2;
        position: relative;
        width: 100px;
        height: $btnH;
        margin: 0 auto;
        background: transparent;
        color: #fff;
        text-transform: uppercase;
        font-size: 15px;
        cursor: pointer;

        &:after {
            content: "";
            z-index: 2;
            position: absolute;
            left: 0;
            top: 0;
            width: 100%;
            height: 100%;
            border: 2px solid #fff;
            border-radius: 30px;
        }

        span {
            position: absolute;
            left: 0;
            top: 0;
            display: flex;
            justify-content: center;
            align-items: center;
            width: 100%;
            height: 100%;
            transition: transform $switchAT;

            &.m--in {
                transform: translateY($btnH * -2);

                @include signUpActive {
                    transform: translateY(0);
                }
            }

            &.m--up {
                @include signUpActive {
                    transform: translateY($btnH * 2);
                }
            }
        }
    }
}

h2 {
    width: 100%;
    font-size: 26px;
    text-align: center;
}

label {
    display: block;
    width: $inputW;
    margin: 25px auto 0;
    text-align: center;

    span {
        font-size: 12px;
        color: #424242;
        text-transform: uppercase;
    }
}

input {
    display: block;
    width: 100%;
    margin-top: 5px;
    padding-bottom: 5px;
    font-size: 16px;
    border-bottom: 1px solid rgba(0, 0, 0, 0.4);
    text-align: center;
}

.forgot-pass {
    margin-top: 15px;
    text-align: center;
    font-size: 12px;
    color: #cfcfcf;
}

.submit {
    margin-top: 40px;
    margin-bottom: 20px;
    background: #4a4a4a;
    text-transform: uppercase;
    transition: all 0.3s;
    &:hover {
        transition: all 0.3s;
        background: #505050;
    }
}

.fb-btn {
    border: 2px solid #d3dae9;
    color: darken(#d3dae9, 20%);

    span {
        font-weight: bold;
        color: darken(#768cb6, 20%);
    }
}

.sign-in {
    transition-timing-function: ease-out;

    @include signUpActive {
        transition-timing-function: ease-in-out;
        transition-duration: $switchAT;
        transform: translate3d($formW, 0, 0);
    }
}

.sign-up {
    transform: translate3d($contW * -1, 0, 0);

    @include signUpActive {
        transform: translate3d(0, 0, 0);
    }
}

.icon-link {
    position: absolute;
    left: 5px;
    bottom: 5px;
    width: 32px;

    img {
        width: 100%;
        vertical-align: top;
    }

    &--twitter {
        left: auto;
        right: 5px;
    }
}
</style>
